package com.gopay.solution.dao.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gopay.common.domain.view.config.View;
import com.gopay.common.domain.view.config.ViewSolutionConfig;
import com.gopay.common.domain.view.config.ViewSolutionTransView;
import com.gopay.common.util.xml.constants.XmlMsgType;
import com.gopay.common.util.xml.tools.XmlToolsService;
import org.springframework.stereotype.Repository;

import com.gopay.solution.domain.po.SsUIConfigDtl;

import javax.annotation.Resource;

/**
 * 方案服务 用户界面配置方案明细
 * @SsUIConfigDtlDAOImpl.java
 * @author liujy
 * @2013-4-22 下午5:51:08  www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("ssUIConfigDtlDAO")
public class SsUIConfigDtlDAOImpl extends
        SolutionRuleDtlDAOImpl<SsUIConfigDtl, Serializable> {

    @Resource(name = "xmlToolsService")
    private XmlToolsService xmlToolsService;

    @Override
    public SsUIConfigDtl getByPk(Serializable id) {
        return find(SsUIConfigDtl.class, id);
    }

    @Override
    public List<SsUIConfigDtl> listBySolutionId(long solutionId) {
        String hql = "from SsUIConfigDtl where solutionId = " + solutionId;
        return findByHql(hql);
    }

    @Override
    public boolean delBySolutionId(long solutionId) {
        List<SsUIConfigDtl> dtls = listBySolutionId(solutionId);
        if (dtls != null && dtls.size() > 0) {
            deleteAll(dtls);
        }
        return true;
    }

    @Override
    public List<SsUIConfigDtl> listBySoluIdAndIntTxnCd(long solutionId, String intTxnCd) {
        //生产6期bug 修改 收银台 个性化展示 2017-7-7 11:45:43
        String hql = "from SsUIConfigDtl dtl where dtl.solutionId = :SolutionId ";

        Map params = new HashMap();
        params.put("SolutionId", solutionId);
        List<SsUIConfigDtl> list = findByHql(hql, params);
        if (list != null) {
            for (SsUIConfigDtl ssUIConfigDtl : list) {
                ViewSolutionConfig config = (ViewSolutionConfig) xmlToolsService.xml2j(
                        XmlMsgType.VIEW_CASHIER_UI_PAYCHANNEL_TYPE, ssUIConfigDtl.getConfigExp());
                List<View> listView = config.getViews();
                for (View view : listView) {
                    if ((view instanceof ViewSolutionTransView) && intTxnCd.equals(((ViewSolutionTransView) view).getIntTxnCd())) {
                        return list;
                    }
                }
                return null;
            }
        }
        return list;
    }

    @Override
    public boolean delBySoluIdAndIntTxnCd(long solutionId, String intTxnCd) {
        return false;
    }
}
